<template>
  <div>
    <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" @ok="handleSubmit">
        <BasicForm @register="registerForm">
          <template #addOrg>
            <a-button type="primary" style="margin-left: 128px;" @click="handleAdd">新增机构</a-button>
          </template>
        </BasicForm>
    </BasicModal>
    <addOrgModal @register="registerOrg" @success="handleSuccess"></addOrgModal>
  </div>
</template>

<script lang="ts" setup>
    import {ref, computed, unref} from 'vue';
    import {BasicModal, useModalInner, useModal} from '/@/components/Modal';
    import {BasicForm, useForm} from '/@/components/Form/index';
    import addOrgModal from './addOrgModal.vue';
    import {formSchema} from '../TSubmission.data';
    import {saveOrUpdate} from '../TSubmission.api';
    
    const [registerOrg, {openModal}] = useModal();
    
    // Emits声明
    const emit = defineEmits(['register','success']);
    const isUpdate = ref(true);
    //表单配置
    const [registerForm, {setProps,resetFields, setFieldsValue, validate, updateSchema}] = useForm({
        //labelWidth: 150,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 24}
    });
    
    const handleTypeChange = async (e) => {
      await setFieldsValue({ bizId: '' });
      if (e.target.value == 1) {
        updateSchema({ field: 'bizId', componentProps: { dict: 't_product,name,no' } });
      } else {
        updateSchema({ field: 'bizId', componentProps: { dict: 't_plot,name,id' } });
      }
    }
    
    //表单赋值
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
        //重置表单
        await resetFields();
        setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter});
        isUpdate.value = !!data?.isUpdate;
        if (unref(isUpdate)) {
            data.record.testOrgId = parseInt(data.record.testOrgId)
            //表单赋值
            await setFieldsValue({
                ...data.record,
            });
            if (data.record.type == 1) {
              updateSchema({ field: 'bizId', componentProps: { dict: 't_product,name,no' } });
            } else if (data.type == 2) {
              updateSchema({ field: 'bizId', componentProps: { dict: 't_plot,name,id' } });
            }
        } else {
          
        }
        // 隐藏底部时禁用整个表单
       setProps({ disabled: !data?.showFooter })
       updateSchema({ field: 'type', componentProps: { onChange: handleTypeChange } });
    });
    //设置标题
    const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
    //表单提交事件
    async function handleSubmit(v) {
        try {
            let values = await validate();
            setModalProps({confirmLoading: true});
            //提交表单
            await saveOrUpdate(values, isUpdate.value);
            //关闭弹窗
            closeModal();
            //刷新列表
            emit('success');
        } finally {
            setModalProps({confirmLoading: false});
        }
    }
    
    /**
      * 新增机构
      */
    function handleAdd() {
       openModal(true);
    }
    
    /**
      * 成功回调
      */
    function handleSuccess() {
        console.log('reload')
        updateSchema({ field: 'testOrgId', componentProps: { params: {t: (new Date()).getTime()} } });
     }
</script>

<style lang="less" scoped>
	/** 时间和数字输入框样式 */
  :deep(.ant-input-number){
		width: 100%
	}

	:deep(.ant-calendar-picker){
		width: 100%
	}
</style>
